﻿using System.Collections.Generic;
using System.Linq;
using Entitas;
class EnemySetRoamingDirectionSystem : ReactiveSystem<EnemyEntity>
{
    readonly System.Random rand;
    public EnemySetRoamingDirectionSystem(Contexts contexts) : base(contexts.enemy)
    {
    }

    protected override void Execute(List<EnemyEntity> entities)
    {
        foreach (var e in entities)
        {
            float angle = e.roamingAngle.value;

            if (0 < angle && angle < 90)
            {
                e.ReplaceDirection(Direction.Up_Right);
            }

            else if (90 < angle && angle < 180)
            {
                e.ReplaceDirection(Direction.Up_Left);
            }

            else if (180 < angle && angle < 270)
            {
                e.ReplaceDirection(Direction.Down_Left);
            }

            else if (270 < angle && angle < 360)
            {
                e.ReplaceDirection(Direction.Down_Right);
            }

            else if (angle == 0)
            {
                e.ReplaceDirection(Direction.Right);
            }

            else if (angle == 90)
            {
                e.ReplaceDirection(Direction.Up);
            }

            else if (angle == 180)
            {
                e.ReplaceDirection(Direction.Left);
            }

            else e.ReplaceDirection(Direction.Down);


        }
    }

    protected override bool Filter(EnemyEntity entity)
    {
        return entity.hasRoamingAngle && entity.isRoaming == true;
    }
    protected override ICollector<EnemyEntity> GetTrigger(IContext<EnemyEntity> context)
    {
        return context.CreateCollector(EnemyMatcher.RoamingAngle.AddedOrRemoved());
    }
}